package cn.project.controller;

import cn.project.model.Page;
import cn.project.model.User;
import cn.project.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private IUserService userService;
    private Page page;
@RequestMapping("/getcond")
public ModelAndView getcond(User user, int pageSize, int pageNum) {
    System.out.println("进来了");
    Map <String, Object> cond = new HashMap <>();
    cond.put("pageSize", pageSize);
    cond.put("pageNum", pageNum);
    cond.put("user", user);
    System.out.println(user.getUsername() + "," + pageSize + "," + pageNum);
    page = userService.queryLikeUsers(cond);
    ModelAndView mv = new ModelAndView();
    mv.setView(new MappingJackson2JsonView());
    mv.addObject("page",page);
    System.out.println(page.getResult().get(0));
    System.out.println("出去");
    return mv;
    }

    @RequestMapping("/delUser")
    public ModelAndView delUser(User user){
    ModelAndView mv=new ModelAndView();

        System.out.println("come on");
    userService.delUser(user);
    mv.setView(new MappingJackson2JsonView());
    return mv;
    }

    @RequestMapping("/findUser")
    public ModelAndView findUser(User user){
        User user1=null;
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        System.out.println("come on find");
        user1=userService.getUser(user);
        mv.addObject("user",user1);
        mv.setViewName("edit_user");
        return mv;
    }


    @RequestMapping("/editUser")
    public ModelAndView editUser(User user){
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        userService.editUser(user);
        return mv;
    }
    @RequestMapping("/addUser")
    public ModelAndView addUser(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("add_user");
        return mv;
    }
    @RequestMapping("/addUsers")
    public ModelAndView addUsers(User user){
        ModelAndView mv=new ModelAndView();
        mv.setView(new MappingJackson2JsonView());
        userService.addUser(user);
        return mv;
    }
    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }

    public IUserService getUserService() {
        return userService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }
}
